package com.dhnsoft.education.service.edu.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dhnsoft.education.entity.edu.EduSubject;
import com.dhnsoft.education.entity.edu.excel.SubjectData;
import com.dhnsoft.education.entity.edu.subject.OneSubject;
import com.dhnsoft.education.entity.edu.subject.TwoSubject;
import com.dhnsoft.education.entity.edu.vo.SubjectNestedVo;
import com.dhnsoft.education.entity.edu.vo.SubjectVo;
import com.dhnsoft.education.listen.SubjectExcelListener;
import com.dhnsoft.education.mapper.edu.EduSubjectMapper;
import com.dhnsoft.education.service.edu.EduSubjectService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author 代浩楠
 * @since 2022-02-17
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        try {
            InputStream in = file.getInputStream();
            EasyExcel.read(in, SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //查询所有一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
        wrapperOne.eq("parent_id","0");
        List<EduSubject> oneSubjectsList  = baseMapper.selectList(wrapperOne);

        //查询所有二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
        wrapperTwo.ne("parent_id","0");
        List<EduSubject> twoSubjectsList = baseMapper.selectList(wrapperTwo);
        //封装一级分类
        //查询出来所有的一级分类list集合遍历，得到每个一级分类对象，获取每个一级分类对象值
        //封装到要求的最终list集合中
        List<OneSubject> finalSubjectList = new ArrayList<>();
        for (int i = 0; i < oneSubjectsList.size(); i++) {
            OneSubject oneSubject = new OneSubject();
            BeanUtils.copyProperties(oneSubjectsList.get(i),oneSubject);
            finalSubjectList.add(oneSubject);
            List<TwoSubject> finalTwoSubjectList = new ArrayList<>();
            for (int m = 0; m < twoSubjectsList.size(); m++) {
                //获取每个二级分类
                EduSubject tSubject = twoSubjectsList.get(m);
                if(tSubject.getParentId().equals(oneSubjectsList.get(i).getId())){
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(twoSubjectsList.get(m),twoSubject);
                    finalTwoSubjectList.add(twoSubject);
                }

            }
            finalSubjectList.get(i).setChildren(finalTwoSubjectList);

        }
        return finalSubjectList;
    }

    @Override
    public List<SubjectNestedVo> getAllSubject(Long count) {
        //1. 查询一级分类
        /*QueryWrapper<EduSubject> wrapper1=new QueryWrapper<>();
        wrapper1.eq("parent_id","0");
        //ServiceImpl中注入baseMapper
        List<EduSubject> eduSubjects1 = baseMapper.selectList(wrapper1);

        QueryWrapper<EduSubject> wrapper2=new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> eduSubjects2 = baseMapper.selectList(wrapper2);*/

        //1.流操作
        //查询所有分类

        List<EduSubject> all = this.list(null);//所有分类
        List<EduSubject> eduSubjects1;
        if(count==null){
            eduSubjects1=all.stream().filter(i -> i.getParentId().equals("0")).collect(Collectors.toList());
        }else{
            eduSubjects1=all.stream().filter(i -> i.getParentId().equals("0")).limit(count).collect(Collectors.toList());
        }
        //一级分类
        List<EduSubject> eduSubjects2 =
                all.stream().filter(i -> !i.getParentId().equals("0")).collect(Collectors.toList());;//二级分类


        return eduSubjects1.stream().map(//一级分类重写map为OneSubject,
                a -> new SubjectNestedVo(a.getId(), a.getTitle(),//OneSubject中的List从eduSubjects2中筛选
                        eduSubjects2.stream().filter(b -> b.getParentId().equals(a.getId()))//filter ParentId等于一级分类的id
                                .map(c -> new SubjectVo(c.getId(), c.getTitle()))//二级分类重写map为TwoSubject
                                .collect(Collectors.toList())))
                .collect(Collectors.toList());
    }
}
